package com.example.webapp.web.controller.forum;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.webapp.service.forum.ForumService;

@Controller
public class ForumController {
	
	private ForumService forumService;
	
	@RequestMapping(value = "forum/forum.html")
	public String showForum(@RequestParam(required = false) String forum, Model model) {
		List<?> forums;
		if (StringUtils.hasLength(forum)) {
			
		}
		
		forums = forumService.getForum(); 
		
		model.addAttribute("forums", forums);
		
		return "forum/forum";
	}
	
//	@RequestMapping(value = "forum/forum.html")
//	public String showForum(Model model) {
//		Session session = hibernateManager.createSession();
//		session.beginTransaction();
//		List<?> result = session.createQuery("from ForumThread").list();
//		
//		model.addAttribute("threads", result);
//		
//		return "forum/forum";
//	}
	
	@RequestMapping(value = "forum/createThread.html", method = RequestMethod.POST)
	public String createThread(@RequestParam String title, @RequestParam String author, Model model) {
//		Session session = hibernateManager.createSession();
//		Transaction t = session.beginTransaction();
//		
//		ForumThread thread = new ForumThread();
//		thread.setTitle(title);
//		thread.setAuthor(author);
//		thread.setDate(Calendar.getInstance().getTimeInMillis());
//		session.save(thread);
//		
//		session.flush();
//		t.commit();
//		session.close();
//		
		return "redirect:forum.html";
	}	
	
	@RequestMapping(value = "forum/thread.html")
	public String showThread(@RequestParam long id, Model model) {
//		Session session = hibernateManager.createSession();
//		session.beginTransaction();
//		
//		ForumThread thread = (ForumThread) session.get(ForumThread.class, id);
//		
//		model.addAttribute("thread", thread);
		
		return "forum/thread";
	}
	
	@RequestMapping(value = "forum/createPost.html", method = RequestMethod.POST)
	public String createPost(@RequestParam String title, @RequestParam String author, @RequestParam String content, @RequestParam long threadId, Model model) {
//		Session session = hibernateManager.createSession();
//		Transaction t = session.beginTransaction();
//		
//		ForumPost post = new ForumPost();
//		post.setAuthor(author);
//		post.setContent(content);
//		post.setDate(Calendar.getInstance().getTimeInMillis());
//		post.setTitle(title);
//		post.setForumThread((ForumThread) session.load(ForumThread.class, threadId));
//		session.save(post);
//		
//		session.flush();
//		t.commit();
//		session.close();
//		
		return "redirect:thread.html?id=" + threadId;
	}
	
	@Required
	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}
}
